// components/signBox/signBox.js
// import { dateCompare, format} from '../../utils/date'
import {
  getToken,getStorageItem
} from '../../utils/storage'

import {
  getSigns,
  toSign
} from '../../api/activity';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: String,
  },
  // observers
  /**
   * 组件的初始数据
   */
  data: {
    signList: [],
    hasSinged: true,
  },
  lifetimes: {
    attached() {
      const isTest = getApp().globalData.isInNationalTest
      const today = new Date()
      const month = today.getMonth() + 1
      if (month !== 10 && !isTest) {
        this.setData({
          hasSinged: true
        })
        return;
      }
      let token = getStorageItem('token')
      if(!token){
        return;
      }
      this.getSignInfo(true)
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    quicklyGoSign() {
      if (this.data.hasSinged) {
        return;
      }
      toSign().then(res => {
          console.log('res------------', res)
          this.getSignInfo(false)
          wx.showToast({
            title: '签到成功',
            icon: 'none'
          })
        })
        .catch(err => {
          wx.showToast({
            title: '签到失败',
            icon: 'none'
          })
          console.log(err)
        })
    },
    getSignInfo(first) {
      getSigns().then(res => {
          console.log('res---', res)
          const {
            size,
            status
          } = res.data
          const signList = new Array(8).fill({}).map((item, index) => {
            let coin = 50;
            let order = index + 1
            let signed = order <= size
            if (Number(size) > 8) {
              let remain = (Number(size) % 8)
              let week = (Number(size) - remain) / 8
              coin = 1000
              order = Number(size) + index

              if (index === 0) {
                signed = status == 1;
              } else {
                signed = false
              }
            } else {
              let coinList = [50, 100, 150, 300, 500, 800, 1000, 1000]
              coin = coinList[index]
            }
            if (status == 1) {
              // this.close()
              if (first) {

                this.setData({
                  hasSinged: true,
                })

              }


            } else {

              this.setData({
                hasSinged: false,
              })
              if (first) {
                this.quicklyGoSign()
              }

            }

            return {
              order,
              coin,
              signed
            }
          })
          console.log('signList', signList)
          this.setData({
            signList,
          })
        })
        .catch(err => {
          console.log(err)
        })
    },
    close() {
      // this.hasSinged = true
      this.triggerEvent('close')

    },
    toMore() {


      this.setData({
        hasSinged: true,
      })
      getApp().globalData.tab = 1;
      wx.switchTab({
        url: '/pages/activity/nationalDay/nationalDay'
      });
    }
  }
})